package com.ms.game.sendgift.db;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import com.xpec.c4.db.basic.BasicGameDAO;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.game.utility.GUID;

public class SendGiftDAO extends BasicGameDAO {

	private static Log log = LogFactory.getLog(SendGiftDAO.class);
	
	@SuppressWarnings("unchecked")
	public static ArrayList<SendGiftNotifyModel> loadAllSendGiftNotifyModelByPlayerID(GUID playerID){
		if (playerID == null) {
			return null;
		}
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("from SendGiftNotifyModel where "
				+ "(playerGUID = :playerID OR targetGUID = :playerID) and taken = :TAKEN "
				+ "order by createTime desc");
		query.setParameter("playerID", playerID.toString());
		query.setParameter("TAKEN", false);
		//query.setParameter("overTime", GameUtility.longToDate(TimeSys.currentTimeMills()));

		ArrayList<SendGiftNotifyModel> model = (ArrayList<SendGiftNotifyModel>) query.list();
		HibernateManager.endSession();
		return model;
	}
	
	public static SendGiftNotifyModel loadSendGiftNotifyModelByOrderID(long orderID){
		if (orderID == 0) {
			return null;
		}
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("from SendGiftNotifyModel where sendOrderID = :orderID");
		query.setParameter("orderID", orderID);

		SendGiftNotifyModel model = (SendGiftNotifyModel) query.uniqueResult();
		HibernateManager.endSession();
		return model;
	}
	
	/**
	 * 載入跟指定 player 有關但還沒結束的寄送禮物交易資料
	 * @param playerID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static ArrayList<SendGiftNotifyModel> loadUpdateMoneyByPlayerID(GUID playerID){
		if (playerID == null) {
			return null;
		}
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("from SendGiftNotifyModel where "
				+ "(playerGUID = :GUID  OR targetGUID = :GUID) and taken =:TAKEN");
		query.setParameter("GUID", playerID.toString());
		query.setParameter("TAKEN", false);

		ArrayList<SendGiftNotifyModel> model = (ArrayList<SendGiftNotifyModel>) query.list();
		HibernateManager.endSession();
		return model;
	}
	
	/**
	 * 保存實體對像 送禮通知
	 * @param entity 待保存的實體對像
	 */
	public static boolean saveOrUpdateGiftNotify(SendGiftNotifyModel entity){
		if(entity==null){
			return false;
		}
		Session session = HibernateManager.txBegin();
		session.saveOrUpdate(entity);
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 保存實體對像 送禮通知
	 * @param entity 待保存的實體對像
	 */
	public static void saveGiftNotify(SendGiftNotifyModel entity){
		Session session=HibernateManager.getSession();
		HibernateManager.txBegin();
		if(entity==null){
			return;
		}
		session.saveOrUpdate(entity);
		HibernateManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 保存多筆禮物通知資料
	 * 
	 * @param playerSocial
	 * @return
	 */
	public static boolean saveOrUpdateListSendGiftNotify(List<SendGiftNotifyModel> models) {
		Session session = HibernateManager.txBegin();
		for(SendGiftNotifyModel model : models){
			session.saveOrUpdate(model);
		}
		return HibernateManager.txCommitFailRollbackClearSession(); 
	}
	
	/**
	 * 更新多筆禮物通知<br>
	 * 
	 * @param guid
	 * @return
	 */
	public static boolean UpdateNotifyFinishList(List<String> list) {
		
		Session session = HibernateManager.txBegin();
		Query query = session
				.createQuery("update SendGiftNotifyModel set finish = :FINISH, finishTime =:TIME where db_id in (:list)");
		query.setParameterList("list", list);
		query.setParameter("FINISH", true);
		query.setParameter("TIME", Calendar.getInstance().getTime());
		query.executeUpdate();
		
		return HibernateManager.txCommitFailRollbackClearSession(); 
	}
	
	/**
	 * 刪除多筆禮物通知資料
	 * 
	 * @param playerSocial
	 * @return
	 */
	public static boolean removeListSendGiftNotify(List<SendGiftNotifyModel> models) {
		Session session = HibernateManager.txBegin();
		for(SendGiftNotifyModel model : models){
			session.delete(model);
		}
		return HibernateManager.txCommitFailRollbackClearSession(); 
	}
	
	/**
	 * 修改禮物通知的createtime，把道具退回給送禮人
	 *  @param OrderId
	 *  @param time
	 */
	public static boolean updateNotifyStageByOrderId(long orderId, int stage){
		Session session = HibernateManager.txBegin();
		Query query = session.createQuery("update SendGiftNotifyModel set stage = :stage where sendOrderID = :orderID");
		query.setParameter("stage", stage);
		query.setParameter("orderID", orderId);
		query.executeUpdate();
		return HibernateManager.txCommitFailRollbackClearSession(); 
	}
}
